<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="养殖户" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入养殖户"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!--      <el-form-item label="日期">
              <el-date-picker
                v-model="daterangeCreatTime"
                size="small"
                style="width: 240px"
                value-format="yyyy-MM-dd"
                type="daterange"
                range-separator="-"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
              ></el-date-picker>
            </el-form-item>-->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['dgw:earnumber:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['dgw:earnumber:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['dgw:earnumber:remove']"
        >删除
        </el-button>
      </el-col>
      <!--      <el-col :span="1.5">
              <el-button
                type="warning"
                plain
                icon="el-icon-download"
                size="mini"
                :loading="exportLoading"
                @click="handleExport"
                v-hasPermi="['dgw:earnumber:export']"
              >导出
              </el-button>
            </el-col>-->
      <el-col :span="1.5" :push="16">
        <span v-model="Sum" style="color: #ffffff; float: right">总存栏:&nbsp;{{ Sum }}&nbsp;头</span>
      </el-col>
      <el-col :span="1.5" :push="10">
        <span v-model="ESum" style="color: #ffffff; float: right">总计智能耳标安装: {{ ESum }}</span>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <el-row :gutter="20">
      <el-col :span="6" :xs="24">
        <el-table height="700" :data="deptList"
                  @row-click="selectRow"
                  :row-style="rowStyle"
                  @selection-change="handleSelectionChange">
          <!--      <el-table-column label="养殖用户id" align="center" prop="userId" />-->
          <el-table-column label="乡镇(12)" align="center" prop="deptName"/>
          <el-table-column label="耳标安装数量" align="center" prop="earNumber"/>


          <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
            <template slot-scope="scope">
              <el-button
                type="primary"
                @click="getSum(scope.row)"

              >查看
              </el-button>
              <!--                    <el-button
                                    size="mini"
                                    type="text"
                                    icon="el-icon-delete"
                                    @click="handleDelete(scope.row)"
                                    v-hasPermi="['dgw:vaccine:remove']"
                                  >删除</el-button>-->
            </template>
          </el-table-column>
        </el-table>
      </el-col>
      <el-col :span="6">
        <el-table height="700" :data="deptNameList"
                  :row-style="rowStyleU"
                  @selection-change="handleSelectionChange">

          <el-table-column :label=label align="center" prop="village"/>
          <el-table-column label="耳标安装数量" align="center" prop="earNumberSum"/>
          <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="primary"
                @click="getUser(scope.row)"
              >查看用户<i class="el-icon-arrow-right el-icon--right"></i></el-button>
              <!--                    <el-button
                                    size="mini"
                                    type="text"
                                    icon="el-icon-delete"
                                    @click="handleDelete(scope.row)"
                                    v-hasPermi="['dgw:vaccine:remove']"
                                  >删除</el-button>-->
            </template>
          </el-table-column>
        </el-table>
      </el-col>
      <el-col :span="12" :xs="24">
        <el-table v-loading="loading" :data="deptNameUserList" @selection-change="handleSelectionChange">
          <!--          <el-table-column type="selection" width="55" align="center"/>-->
          <el-table-column label="养殖户" align="c enter" prop="user_name"/>
          <el-table-column label="电话" align="center" prop="phone" width="120"/>
          <el-table-column label="存栏量(头)" align="center" prop="restock_sum"/>
          <el-table-column label="应装耳标" align="center" prop="restock_sum"/>
          <el-table-column label="已装耳标" align="center" prop="ear_number"/>

          />
          <!--          <el-table-column label="部门id" align="center" prop="deptId" />-->
          <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                icon="el-icon-edit"
                @click="ClickEar(scope.row)"
              >查看耳标编号
              </el-button>
            </template>
          </el-table-column>
        </el-table>

        <pagination
          v-show="total>0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
      </el-col>
    </el-row>


    <!-- 添加或修改耳标数量对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="养殖户" prop="name">
          <el-input v-model="form.name" placeholder="请输入养殖户"/>
        </el-form-item>
        <el-form-item label="耳标总数" prop="earNum">
          <el-input v-model="form.earNum" placeholder="请输入耳标总数"/>
        </el-form-item>
        <el-form-item label="地点" prop="addess">
          <el-input v-model="form.addess" placeholder="请输入地点"/>
        </el-form-item>
        <el-form-item label="电话
" prop="phone"
        >
          <el-input v-model="form.phone" placeholder="请输入电话
"
          />
        </el-form-item>
        <el-form-item label="是否交款  1=未交款，2=已交款">
          <el-radio-group v-model="form.status">
            <el-radio label="1">请选择字典生成</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="日期" prop="creatTime">
          <el-date-picker clearable size="small"
                          v-model="form.creatTime"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="选择日期"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="部门id" prop="deptId">
          <el-input v-model="form.deptId" placeholder="请输入部门id"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 查询耳标信息 -->
    <el-dialog :title="titleEar" :visible.sync="openEar" width="800px" append-to-body>
      <el-table v-loading="loading" height="700" :data="earnumberList"
                @row-click="selectRow"
                :row-style="rowStyle"
                @selection-change="handleSelectionChange">
        <!--      <el-table-column label="养殖用户id" align="center" prop="userId" />-->
        <el-table-column label="耳标编号" align="center" prop="earNumber"/>
        <el-table-column label="耳标安装日期" align="center" prop="createTime"/>
        <el-table-column label="牛月龄" align="center" prop="isUse">
          <template slot-scope="scope">
            <span>{{ scope.row.isUse }}个月</span>
          </template>
        </el-table-column>
        <el-table-column label="安装人员" align="center" prop="createBy"/>
        <el-table-column label="预出栏时间" align="center" prop="cowTime"/>
      </el-table>
    </el-dialog>
  </div>
</template>

<script>
import {
  queryEarList, getEarnumber,
  delEarnumber,
  addEarnumber,
  updateEarnumber,
  exportEarnumber, queryList, querySum
} from '@/api/dgw/earnumberTable'
import {queryDeptCun, treeselect} from '@/api/system/dept'
import {listQueryVillage, listQueryVillageUser, queryVSum} from '@/api/dgw/registration'

export default {
  name: 'Earnumber',
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      Sum: null,
      ESum: 267,
      label: '村庄(**)',
      // 耳标数量表格数据
      earnumberList: [],
      deptList: [],
      deptNameList: [],
      deptNameUserList: [],
      name: null,
      names: null,
      // 弹出层标题
      title: '',
      titleEar: '',
      // 是否显示弹出层
      open: false,
      openEar: false,
      //地区数据
      deptName: undefined,
      // 地区数据树选项
      deptOptions: undefined,
      // 部门id时间范围
      daterangeCreatTime: [],
      defaultProps: {
        children: 'children',
        label: 'label'
      },
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        village: null
      },
      queryParam: {
        villagesTowns: null
      },
      queryParamUser: {
        village: null,
        villagesTowns: null,
      },
      queryParamSum: {
        deptId: null
      },
      queryParamEar: {
        name: null,
        deptName: null,
        phone: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {}
    }
  },
  created() {
    this.getList()
    this.getTreeselect()
    this.getQueryDeptCun()
  },
  methods: {
    /** 查询耳标数量列表 */
    getList() {
      /* this.loading = true;
       listEarnumber(this.queryParams).then(response => {
         this.earnumberList = response.rows;
         this.total = response.total;
         this.loading = false;
       });*/
      queryVSum(this.queryParamUser).then(response => {
        this.Sum = response.data.sum
      })
      querySum(this.queryParamSum).then(response => {
        this.ESum = response.data.sum
      })
    },
    getQueryDeptCun() {
      this.loading = true
      queryDeptCun().then(response => {
        this.deptList = response.data
        this.loading = false
      })
    },
    getQueryDeptName() {
      this.loading = true
      listQueryVillage(this.queryParam).then(response => {
        this.label = '村庄(' + response.total + ')'
        this.deptNameList = response.rows
        this.loading = false
      })
    },
    getQueryDeptUser() {
      this.loading = true
      queryList(this.queryParams).then(response => {
        this.deptNameUserList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        earNum: null,
        addess: null,
        phone: null,
        status: 0,
        creatTime: null,
        deptId: null
      }
      this.resetForm('form')
    },
    /** 查询部门下拉树结构 */
    getTreeselect() {
      treeselect().then(response => {
        this.deptOptions = response.data[0].children[0].children[0].children
        console.log('部门', response.data[0].children[0].children)
      })
    },
    // 筛选节点
    filterNode(value, data) {
      if (!value) return true
      return data.label.indexOf(value) !== -1
    },
    // 节点单击事件
    handleNodeClick(data) {
      console.log('点击：{}', data)
      this.queryParams.deptId = data.id
      this.getList()
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeCreatTime = []
      this.resetForm('queryForm')
      this.deptName = undefined,
        this.queryParamUser.village = null,
        this.queryParamUser.villagesTowns = null,
        this.queryParam.villagesTowns = null,
        this.queryParams.village = null,
        this.queryParamSum = null

      this.names = null
      this.name = null

      this.names = null
      this.name = null
      this.getQueryDeptName()
      this.handleQuery()
      this.getList()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加耳标数量'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      getEarnumber(id).then(response => {
        this.form = response.data
        this.open = true
        this.title = '修改耳标数量'
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateEarnumber(this.form).then(response => {
              this.$modal.msgSuccess('修改成功')
              this.open = false
              this.getList()
            })
          } else {
            addEarnumber(this.form).then(response => {
              this.$modal.msgSuccess('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal.confirm('是否确认删除耳标数量编号为"' + ids + '"的数据项？').then(function () {
        return delEarnumber(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess('删除成功')
      }).catch(() => {
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams
      this.$modal.confirm('是否确认导出所有耳标数量数据项？').then(() => {
        this.exportLoading = true
        return exportEarnumber(queryParams)
      }).then(response => {
        this.download(response.msg)
        this.exportLoading = false
      }).catch(() => {
      })
    },
    /*查询村*/
    getSum(row) {
      console.log("11111", row)
      this.queryParam.villagesTowns = row.deptName

      this.getQueryDeptName()
      this.queryParamSum.deptId = row.deptId
      this.queryParamUser.villagesTowns = row.deptName
      this.queryParamUser.village = null
      // this.queryParamUser.villagesTowns =row.deptName;
      this.getList()
    },
    getUser(row) {
      this.queryParams.village = row.village
      this.getQueryDeptUser()
      this.names = row.village
      this.queryParamUser.village = row.village
      this.queryParamUser.villagesTowns = null
      this.queryParamSum.deptId = row.deptId
      this.getList()
    },
    // 行鼠标点击事件
    selectRow(row, column, event) {
     /* console.log(row);
      console.log(column);
      console.log(event);*/
      this.name = row.deptName;
    },
    // 更改选中行背景色
    rowStyle({row}) {
      if (this.name === row.deptName) {
        return {'background-image': 'linear-gradient(-60deg,#16a085 0%,#f4d03f 100%)', cursor: 'pointer'};
      }
      return {cursor: 'pointer'};
    },
    rowStyleU({row}) {
      if (this.names === row.village) {
        return {'background-image': 'linear-gradient(-60deg,#16a085 0%,#f4d03f 100%)', cursor: 'pointer'};
      }
      return {cursor: 'pointer'};
    },
    //查询耳标编号
    ClickEar(row) {
      this.openEar = true
      this.titleEar = '耳标信息(' + row.user_name + ')',
        this.queryParamEar.name = row.user_name;
      this.queryParamEar.deptName = row.village;
      this.queryParamEar.phone = row.phone;
      queryEarList(this.queryParamEar).then(response => {
        console.log("耳标信息：", response)
        this.earnumberList = response.rows
      })
    }
  },
  watch: {
    // 根据名称筛选部门树
    deptName(val) {
      this.$refs.tree.filter(val)
    }
  }
}
</script>
<style lang="scss" scoped>
/*取消表格鼠标进入高亮显示*/
.el-table__row:hover > td {
  background-color: transparent;
}
</style>
